// pages/orderDetail/orderDetail.js
const {
  orderById,
  orderAddress,
  orderLogistics,
  imageBaseUrl
} = require('../../api/url.js')
import {
  formatOrderTime,
  formatRegion
} from '../../utils/util.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    orderDetail: {},
    address: {},
    logistics: null,
    isIPX:false,//是否是iPhoneX
  },

  //读取订单信息
  loadOrderDetail(orderId) {
    wx.showLoading({
      title: '加载中',
    })
    var self = this
    const {
      user
    } = this.data
    wx.request({
      url: orderById,
      data: {
        fId: orderId,
        token: wx.getStorageSync("token"),
        loginName: user.loginname,
        fUserId: user.id
      },
      success: function(res) {
        if (res.data.code == 403) {
          wx.hideLoading()
        } else if (res.data.code == 1) {
          wx.hideLoading()
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        } else if (res.data.code == 0) {
          wx.hideLoading()
          console.log('打印res')
          console.log(res)
          var data = res.data.response.cont
          data.list.map(i => {
            if (i.productSku && i.productSku.fPrice) { //如果有sku则单个商品总价等于sku里的单价*数量
              i.priceTxt = i.productSku.fPrice.toFixed(2)
            } else { //如果没有sku则商品总价等于外层的单价*数量
              i.priceTxt = i.fPrice.toFixed(2)
            }
            if (i.productSku && i.productSku.skuPropertyValueList && i.productSku.skuPropertyValueList.length > 0) { //如果有sku则图片等于sku里的图片，sku等于sku内容
              i.skuTxt = i.productSku.skuPropertyValueList.map(j => (j.propertyName + ':' + j.fName)).join(';')
            } else { //如果没有sku，sku为空，图片为外层图片
              i.skuTxt = ''
            }
            i.img = imageBaseUrl + i.productImage.fThumbImage
          })
          data.totalPriceText = data.fOrderAmount.toFixed(2)
          if (typeof data.fCreatedatatime == 'number'){
            data.createTime = formatOrderTime(data.fCreatedatatime) //格式化时间
          }else{
            data.createTime = data.fCreatedatatime //格式化时间
          }
          self.setData({
            orderDetail: data
          })
          wx.request({ //根据订单里的地址id查询订单对应地址信息
            url: orderAddress,
            data: {
              fId: self.data.orderDetail.fAddressId,
              token: wx.getStorageSync("token"),
              loginName: user.loginname,
              fUserId: user.id
            },
            success: function(res) {
              var addressDetail = res.data.response.cont
              addressDetail.region = formatRegion(addressDetail.fProvinceNum, addressDetail.fCityNum, addressDetail.fAreaNum) //格式化地区
              self.setData({
                addresss: addressDetail
              })
            }
          })
          wx.request({ //根据订单里id查询订单对应物流信息
            url: orderLogistics,
            data: {
              fOrderId: self.data.orderDetail.fId,
              token: wx.getStorageSync("token"),
              loginName: user.loginname,
              fUserId: user.id
            },
            success: function(res) {
              self.setData({
                logistics: res.data.response.cont || null
              })
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      user: app.globalData.userInfo,
      isIPX:app.globalData.isIPX
    })
    if (options.id) {
      this.loadOrderDetail(options.id)
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})